package pkg

import (
	"bufio"
	"fmt"
	"log"
	"os"
)

func ReadFile(filepath string) error {
	file, err := Openfiles(filepath)
	if err != nil {
		log.Printf("open file %s err: %s\n", filepath, err)
		return err
	}

	reader := bufio.NewReader(file)

	for {
		line, err := reader.ReadString('\n')
		if err != nil {
			break
		}
		fmt.Printf(line)
	}

	return nil
}

func WriteFile(data string, filepath string) error {

	file, err := Openfiles(filepath)
	if err != nil {
		log.Printf("open file %s err: %s\n", filepath, err)
		return err
	}

	writer := bufio.NewWriter(file)

	// 未完成
	writer.WriteString(data)

	return nil
}

func Openfiles(filepath string) (*os.File, error) {
	file, err := os.OpenFile(filepath, os.O_RDWR, 0644)

	return file, err
}
